<?php

// @formatter:off
/**
 * @file directory.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: directory.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/services/tags/remove/directory.php $
 */
// @formatter:on

require_once __DIR__."/../tools.php";

class WCServiceTagsRemoveDirectory extends WCService {
	//
	// Protected variables.
	protected $_tags = array(
		"tags" => array(),
		"required" => array()
	);
	//
	// Class methods.
	public function __construct() {
		parent::__construct();

		$this->_readOnlyConflict = true;

		$this->_requiredParams["url"][] = "group";
		$this->_requiredParams["url"][] = "directory";

		$this->_requiredParams["params"][] = "list";

		$this->_requiredPermissions["to"][] = WC_PERM_TAG_LIST;
		$this->_requiredPermissions["to"][] = WC_PERM_DIRECTORY_SET_TAG;
	}
	//
	// Public methods
	public function check() {
		if(parent::check()) {
			global $wcDirectory;
			global $wcGroup;

			if(count($this->list) < 1) {
				$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "No tags to remove");
			}
			if(!$wcGroup || !$wcGroup->ok()) {
				$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "There's something wrong with the specified group id");
			}
			if(!$wcDirectory || !$wcDirectory->ok()) {
				$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "There's something wrong with the specified directory id");
			}
			if($this->ok() && $wcGroup->id != $wcDirectory->group) {
				$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "Unmatching groups");
			}
		}

		return $this->ok();
	}
	//
	// Protected methods.
	protected function load() {
		$out = false;

		if(parent::load()) {
			$this->list = is_array($this->list) ? $this->list : array();

			$this->_tags = array();
			$holder = WCTagsHolder::I();
			foreach($this->list as $id) {
				$aux = $holder->item($id);
				if($aux && $aux->ok()) {
					$this->_tags[] = $aux;
				}
			}

			$out = true;
		}

		return $out;
	}
	protected function runPOST() {
		global $wcDirectory;

		foreach($this->_tags as $tag) {
			$ok = $tag->removeDirectory($wcDirectory);
			$this->_count += $ok ? 1 : 0;
		}
	}
}
